package com.qt.thread.homework.work3;

import com.qt.zipstream.utils.StringUtil;
import org.apache.commons.lang3.StringUtils;

import java.util.*;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.stream.Collectors;

/**
 * @Author zsf
 * @Date 2023/10/4 1:34
 * @Version 1.0
 */
public class Demo03 {

    /**
     * 练习多线程练习6(多线程统计并求最大值)
     * 在上一题基础上继续完成如下需求:
     * 每次抽的过程中，不打印，抽完时一次性打印(随机)
     * 在此次抽奖过程中，抽奖箱1总共产生了6个奖项。
     * 分别为:10,20,100,500,2,300最高奖项为300元，总计额为932元在此次抽奖过程中，
     * 抽奖箱2总共产生了6个奖项。
     * 分别为:5,50,200,800,80,700最高奖项为800元，总计额为1835元
     */

    public static void main(String[] args) {

        Integer[] arr = {10,5,20,50,100,200,500,800,2,80,300,700};
        //装箱
        List<Integer> integers = Arrays.stream(arr).collect(Collectors.toList());


        // 创建两个抽奖箱
        LotteryBox box1 = new LotteryBox(integers);
        LotteryBox box2 = new LotteryBox(integers);
        // 创建两个线程
        FutureTask<HashMap> ft1 = new FutureTask<HashMap>(box1);
        FutureTask<HashMap> ft2 = new FutureTask<HashMap>(box2);

        Thread thread1 = new Thread(ft1, "抽奖盒1");
        Thread thread2 = new Thread(ft2, "抽奖盒1");

        // 启动两个线程
        thread1.start();
        thread2.start();

        try {
            StringBuilder sb = new StringBuilder();
            while (true){
                HashMap map1 = ft1.get();
                HashMap map2 = ft2.get();
                if(map1 != null && map2 != null){
                    List<Integer> list1 = (List) map1.get("list");
                    List<Integer> list2 = (List) map2.get("list");
                    if(list1.size() == 5 && list2.size() == 5){
                        Optional<Integer> max1 = list1.stream().max((o1, o2) -> o1 - o2);
                        Optional<Integer> max2 = list2.stream().max((o1, o2) -> o1 - o2);
                        sb.append(map1.get("name") + "共产生了" + list1.size() + "个奖项.分别为：" + list1.toString() + "最高奖项为" + max1.get() + "总金额：" + map1.get("sum") + "\n");
                        sb.append(map2.get("name") + "共产生了" + list2.size() + "个奖项.分别为：" + list2.toString() + "最高奖项为" + max2.get() + "总金额：" + map2.get("sum"));
                        System.out.println(sb.toString());
                        break;
                    }

                }
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }

    }



}
